package com.wlhse.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.wlhse.dao.EchartsDao;
import com.wlhse.dao.OversightDao;
import com.wlhse.dto.inDto.*;
import com.wlhse.dto.outDto.*;
import com.wlhse.service.EchartService;
import com.wlhse.util.AccountDateUtil;
import com.wlhse.util.DateUtil;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class EchartServiceImpl implements EchartService {


    @Resource
    private EchartsDao echartsDao;

    @Resource
    private OversightDao oversightDao;

    @Override
    public String getProblemLevelAnalysisOutDtoList(ProblemLevelAnalysisInDto problemLevelAnalysisInDto) {
//        if (StringUtils.isNotBlank(problemLevelAnalysisInDto.getStartTime()) && StringUtils.isBlank(problemLevelAnalysisInDto.getEndTime())) {
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//            problemLevelAnalysisInDto.setEndTime(df.format(new Date()));
//        }
        return NR.r(echartsDao.getProblemLevelAnalysis(problemLevelAnalysisInDto));
    }

    @Override
    public String getProblemTrendOutDtoList(ProblemTrendInDto problemTrendInDto) {
        if (StringUtils.isBlank(problemTrendInDto.getStartDate())) {
            problemTrendInDto.setStartDate(DateUtil.getTime(CodeDict.TIME_FIRSTDAY));
        }
        if (StringUtils.isBlank(problemTrendInDto.getEndDate())) {
            problemTrendInDto.setEndDate(DateUtil.getTime(CodeDict.TIME_LASTDAY));
        }
        return NR.r(echartsDao.getProblemTrendOutDto(problemTrendInDto));
    }

    @Override
    public String getOversightByCompanyOutDto(OversightInDto oversightInDto) {
//        if ((StringUtils.isNotBlank(oversightInDto.getStartDate()) && StringUtils.isBlank(oversightInDto.getEndDate()))) {
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//            oversightInDto.setEndDate(df.format(new Date()));
//        }
        if (oversightInDto.getType().equals("all")) {
            return NR.r(oversightDao.queryOversightByCompany(oversightInDto));
        } else {
            Integer total = oversightDao.queryTotalOversightByCompany(oversightInDto);
            PageHelper.startPage(oversightInDto.getPageIdx(), oversightInDto.getPageSize());
            List<OversightByCompanyOutDto> oversightByCompanyOutDtos = oversightDao.queryOversightByCompany(oversightInDto);
            return NR.r(oversightByCompanyOutDtos, total, oversightInDto.getPageIdx());
        }
    }

    @Override
    public String getOversightByEmployeeOutDto(OversightInDto oversightInDto) {
//        if (StringUtils.isNotBlank(oversightInDto.getStartDate()) && StringUtils.isBlank(oversightInDto.getEndDate())) {
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//            oversightInDto.setEndDate(df.format(new Date()));
//        }
        if (oversightInDto.getType().equals("all")) {
            return NR.r(oversightDao.queryOversightByEmployee(oversightInDto));
        } else {
            Integer total = oversightDao.queryTotalOversightByEmployee(oversightInDto);
            PageHelper.startPage(oversightInDto.getPageIdx(), oversightInDto.getPageSize());
            List<OversightByEmployeeOutDto> oversightByEmployeeOutDtos = oversightDao.queryOversightByEmployee(oversightInDto);
            return NR.r(oversightByEmployeeOutDtos, total, oversightInDto.getPageIdx());
        }
    }

    @Override
    public String getEchartTaskAndProcessOutDto(OversightInDto oversightInDto) {
//        if (StringUtils.isNotBlank(oversightInDto.getStartDate()) && StringUtils.isBlank(oversightInDto.getEndDate())) {
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//            oversightInDto.setEndDate(df.format(new Date()));
//        }
        return NR.r(oversightDao.queryEchartTaskAndProcess(oversightInDto));
    }

    @Override
    public String getEchartResponseEmp(OversightInDto oversightInDto) {
//        if (StringUtils.isNotBlank(oversightInDto.getStartDate()) && StringUtils.isBlank(oversightInDto.getEndDate())) {
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//            oversightInDto.setEndDate(df.format(new Date()));
//        }
        return NR.r(oversightDao.queryEchartResponseEmp(oversightInDto));
    }

    @Override
    public String getCompanyProblemAnalysis(CompanyProblemAnalysisInDto companyProblemAnalysisInDto) {
//        if (StringUtils.isNotBlank(companyProblemAnalysisInDto.getStartDate()) && StringUtils.isBlank(companyProblemAnalysisInDto.getEndDate())) {
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//            companyProblemAnalysisInDto.setEndDate(df.format(new Date()));
//        }
        return NR.r(echartsDao.getCompanyProblemAnalysisOutDto(companyProblemAnalysisInDto));
    }

    @Override
    public String getIndexSystemOutDto(IndexSystemInDto indexSystemInDto) {
        String startDate = indexSystemInDto.getStartDate();
        String endDate = indexSystemInDto.getEndDate();
        SimpleDateFormat df = new SimpleDateFormat("yyyy");
        if (StringUtils.isBlank(startDate)) {
            startDate = df.format(new Date()) + "-1-1";
        }
        if (StringUtils.isBlank(endDate)) {
            endDate = df.format(new Date()) + "-12-31";
        }
        String code = indexSystemInDto.getCheckItem();
        if (StringUtils.isBlank(code)) {
            List<IndexSystemOutDto> list1 = echartsDao.getIndexSystemOutDto1("", indexSystemInDto.getCompanyCode(), startDate, endDate);
            List<IndexSystemOutDto> list2 = echartsDao.getIndexSystemOutDto("", indexSystemInDto.getCompanyCode(), startDate, endDate);
            list1.addAll(list2);
            return NR.r(list1);
        }
        if (code.toCharArray()[0] == 'A')
            return NR.r(echartsDao.getIndexSystemOutDto1(code, indexSystemInDto.getCompanyCode(), startDate, endDate));
        else
            return NR.r(echartsDao.getIndexSystemOutDto(code, indexSystemInDto.getCompanyCode(), startDate, endDate));
    }

    @Override
    public String getProblemRepeatOutDto() {
        String firstDay = DateUtil.getTime(CodeDict.TIME_FIRSTDAY);
        String lastDay = DateUtil.getTime(CodeDict.TIME_LASTDAY);
        return NR.r(echartsDao.getProblemRepeatOutDto(firstDay, lastDay));
    }

    @Override
    public String getDayEchratOutDto() {
        String startDate = DateUtil.geLastWeekMonday();
        String endDate = DateUtil.getThisWeekLastDay();
        List<DayEchratOutDto> dayEchratOutDtoList = echartsDao.getDayEchratOutDto(startDate, endDate);
        String lastWeekLastDay = DateUtil.getLastWeekLastDay();
        List<String> list = AccountDateUtil.getEveryday(startDate, endDate);
        HashMap<String, DayEchratOutDto> map = new HashMap<>();
        for (DayEchratOutDto dto : dayEchratOutDtoList) {
            map.put(dto.getDays(), dto);
        }
        List<DayEchratOutDto> dayEchratOutDtoList1 = new ArrayList<>();
        for (String nowDay : list) {
            if (map.get(nowDay) == null) {
                DayEchratOutDto dayEchratOutDto = new DayEchratOutDto();
                dayEchratOutDto.setDays(nowDay);
                if (DateUtil.compareDateInOneWeek(nowDay, lastWeekLastDay)) {
                    dayEchratOutDto.setLable("上周" + DateUtil.dayForWeek(nowDay));
                } else {
                    dayEchratOutDto.setLable("本周" + DateUtil.dayForWeek(nowDay));
                }
                dayEchratOutDtoList1.add(dayEchratOutDto);
            } else {
                if (DateUtil.compareDateInOneWeek(nowDay, lastWeekLastDay)) {
                    map.get(nowDay).setLable("上周" + DateUtil.dayForWeek(nowDay));
                } else {
                    map.get(nowDay).setLable("本周" + DateUtil.dayForWeek(nowDay));
                }
                dayEchratOutDtoList1.add(map.get(nowDay));
            }
        }
        return NR.r(dayEchratOutDtoList1);

    }

    //根据月份，分析今年和去年的事故事件  唐欢+曹玉双
    @Override
    public String getAccidentRecordByMonth(CompanyProblemAnalysisInDto companyProblemAnalysisInDto) {

        if (StringUtils.isNotBlank(companyProblemAnalysisInDto.getStartDate()) && StringUtils.isBlank(companyProblemAnalysisInDto.getEndDate())) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            companyProblemAnalysisInDto.setEndDate(df.format(new Date()));
        }
        if (StringUtils.isBlank(companyProblemAnalysisInDto.getStartDate())) {
            companyProblemAnalysisInDto.setStartDate(DateUtil.getTime(CodeDict.TIME_FIRSTDAY));
        }
        if (StringUtils.isBlank(companyProblemAnalysisInDto.getEndDate())) {
            companyProblemAnalysisInDto.setEndDate(DateUtil.getTime(CodeDict.TIME_LASTDAY));
        }
        String tag = "";
        if (companyProblemAnalysisInDto.getPageName().equals("事故")) {
            tag = "accidentrecord";
        } else if (companyProblemAnalysisInDto.getPageName().equals("事件")) {
            tag = "eventrecord";
        }
        //获取当前年份和前一年的事故事件
        //前端传递开始时间和结束时间
        String startTime = companyProblemAnalysisInDto.getStartDate();
        String endTime = companyProblemAnalysisInDto.getEndDate();
        int startMonth = Integer.parseInt(startTime.split("-")[1]);//开始月
        int endMonth = Integer.parseInt(endTime.split("-")[1]);//结束月
        //前一年开始时间和结束时间
        String beforeStartTime = (Integer.parseInt(startTime.split("-")[0]) - 1) + startTime.split("-")[1] + startTime.split("-")[2];
        String beforeEndTime = (Integer.parseInt(endTime.split("-")[0]) - 1) + endTime.split("-")[1] + endTime.split("-")[2];

        //当前年查询数据
        List<IndexSystemOutDto> list1 = echartsDao.getAccidentRecordByMonth(tag, companyProblemAnalysisInDto.getCompanyCode(), companyProblemAnalysisInDto.getStartDate(), companyProblemAnalysisInDto.getEndDate(), companyProblemAnalysisInDto.getIsOrderBy());
        //前一年查询数据
        List<IndexSystemOutDto> list2 = echartsDao.getAccidentRecordByMonth(tag, companyProblemAnalysisInDto.getCompanyCode(), beforeStartTime, beforeEndTime, companyProblemAnalysisInDto.getIsOrderBy());
        //前一年
        List<IndexSystemOutDto> list2Result = new ArrayList<>();
        List<IndexSystemOutDto> list1Result = new ArrayList<>();
        //前一年和当前年开始月和结束月之间所有count置为0
        for (int i = startMonth; i <= endMonth; i++) {
            IndexSystemOutDto indexSystemOutDto2 = new IndexSystemOutDto();//前一年
            IndexSystemOutDto indexSystemOutDto1 = new IndexSystemOutDto();
            indexSystemOutDto2.setCount(0);
            indexSystemOutDto2.setLabel("ByMonth");
            indexSystemOutDto2.setMonths(Integer.parseInt(startTime.split("-")[0]) - 1 + "-0" + i);
            indexSystemOutDto1.setCount(0);
            indexSystemOutDto1.setLabel("ByMonth");
            indexSystemOutDto1.setMonths(endTime.substring(0, 5) + "0" + i);
            list2Result.add(indexSystemOutDto2);
            list1Result.add(indexSystemOutDto1);
        }
        //前一年数据库查到count覆盖原始0
        for (IndexSystemOutDto in : list2) {//数据库结果集
            for (IndexSystemOutDto in2 : list2Result) {//返回结果
                if (in.getMonths().equals(in2.getMonths())) {
                    in2.setCount(in.getCount());
                }
            }
        }
        //当前年数据库查到count覆盖原始0
        for (IndexSystemOutDto in : list1) {//数据库结果集
            for (IndexSystemOutDto in2 : list1Result) {//返回结果
                if (in.getMonths().equals(in2.getMonths())) {
                    in2.setCount(in.getCount());
                }
            }
        }


        JSONObject object = new JSONObject(true);
        object.put("code", CodeDict.SUCCESS);
        object.put("message", "操作成功");
        object.put("list_before", list2Result);
        object.put("list_now", list1Result);
        return object.toJSONString();

    }

    //根据类别分析事故事件
    @Override
    public String getAccidentRecordByType(CompanyProblemAnalysisInDto companyProblemAnalysisInDto) {

        String tag = "";
        if (companyProblemAnalysisInDto.getPageName().equals("事故")) {
            tag = "accidentrecord";
        } else if (companyProblemAnalysisInDto.getPageName().equals("事件")) {
            tag = "eventrecord";
        }
        List<IndexSystemOutDto> list = echartsDao.getAccidentRecordByType(tag, companyProblemAnalysisInDto.getCompanyCode(), companyProblemAnalysisInDto.getStartDate(), companyProblemAnalysisInDto.getEndDate());
        return NR.r(list, 0, 0);

    }

    //根据公司分析事故事件
    @Override
    public String getAccidentRecordByCom(CompanyProblemAnalysisInDto companyProblemAnalysisInDto) {

        String tag = "";
        if (companyProblemAnalysisInDto.getPageName().equals("事故")) {
            tag = "accidentrecord";
        } else if (companyProblemAnalysisInDto.getPageName().equals("事件")) {
            tag = "eventrecord";
        }
        String companyCode = StringUtils.isNotBlank(companyProblemAnalysisInDto.getCompanyCode()) ? companyProblemAnalysisInDto.getCompanyCode() : "00";
        List<IndexSystemOutDto> list = echartsDao.getAccidentRecordByCom(tag, companyCode, companyProblemAnalysisInDto.getStartDate(), companyProblemAnalysisInDto.getEndDate());
        return NR.r(list, 0, 0);

    }

    @Override
    public String getCountRecordByMonth(CompanyProblemAnalysisInDto companyProblemAnalysisInDto) {

        String tag = "";
        if (companyProblemAnalysisInDto.getPageName().equals("事故")) {

            tag = "accidentrecord";
        } else if (companyProblemAnalysisInDto.getPageName().equals("事件")) {

            tag = "eventrecord";
        } else if (companyProblemAnalysisInDto.getPageName().equals("问题")) {

            tag = "problem";
        }

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String temp = df.format(new Date());
        String startTime1 = temp.split("-")[0] + "-01-01";
        String startTime2 = Integer.parseInt(temp.split("-")[0]) - 1 + "-01-01";

        String endTime1 = Integer.parseInt(temp.split("-")[0]) + "-12-31";
        String endTime2 = Integer.parseInt(temp.split("-")[0]) - 1 + "-12-31";
        String month[] = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        //去年
        List<IndexSystemOutDto> list2 = echartsDao.getAccidentRecordByMonth(tag, companyProblemAnalysisInDto.getCompanyCode(), startTime2, endTime2, companyProblemAnalysisInDto.getIsOrderBy());
        //今年
        List<IndexSystemOutDto> list1 = echartsDao.getAccidentRecordByMonth(tag, companyProblemAnalysisInDto.getCompanyCode(), startTime1, endTime1, companyProblemAnalysisInDto.getIsOrderBy());
        List<IndexSystemOutDto> list2Result = new ArrayList<>();
        List<IndexSystemOutDto> list1Result = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            IndexSystemOutDto indexSystemOutDto2 = new IndexSystemOutDto();
            IndexSystemOutDto indexSystemOutDto1 = new IndexSystemOutDto();
            indexSystemOutDto2.setCount(0);
            indexSystemOutDto2.setLabel("ByMonth");
            indexSystemOutDto2.setMonths(Integer.parseInt(endTime1.split("-")[0]) - 1 + "-" + month[i]);
            indexSystemOutDto1.setCount(0);
            indexSystemOutDto1.setLabel("ByMonth");
            indexSystemOutDto1.setMonths(endTime1.substring(0, 5) + month[i]);
            list2Result.add(indexSystemOutDto2);
            list1Result.add(indexSystemOutDto1);
        }
        for (IndexSystemOutDto in : list2) {//数据库结果集
            for (IndexSystemOutDto in2 : list2Result) {//返回结果
                if (in.getMonths().equals(in2.getMonths())) {
                    in2.setCount(in.getCount());
                }
            }
        }
        for (IndexSystemOutDto in : list1) {//数据库结果集
            for (IndexSystemOutDto in2 : list1Result) {//返回结果
                if (in.getMonths().equals(in2.getMonths())) {
                    in2.setCount(in.getCount());
                }
            }
        }
        JSONObject object = new JSONObject(true);
        object.put("code", CodeDict.SUCCESS);
        object.put("message", "操作成功");
        object.put("list_before", list2Result);
        object.put("list_now", list1Result);
        return object.toJSONString();

    }

    @Override
    public String getCountAccident(String pageName) {
        String tag = "";
        if (pageName.equals("事故")) {
            tag = "accidentrecord";
        } else if (pageName.equals("事件")) {
            tag = "eventrecord";
        } else if (pageName.equals("问题")) {
            tag = "problem";
        }
        return NR.r(echartsDao.getCountAccident(tag));
    }
}
